package yxy.game.pm2.module.achievement;

import java.util.Collection;

import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.message.TaskMessage.AchievementBonusReceiveResponse.Builder;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.task.TaskHandle;

public class AchievementHandle {

	public Result reward(String playerCode, String taskCode, AchievementModule module, Builder response) {
		try {
			if (Task.lockTask(playerCode) && AchievementModule.lockAch(playerCode)) {

				for (String code : module.getTaskCodes()) {
					if (code.equals(taskCode)) {
						Task task = Task.get(playerCode, taskCode);
						if (!task.isFinish() || task.isReward()) {
							return Result.TaskCantReward;// 任务奖励不能领取
						}

						Collection<Currency> rewards = new TaskHandle().rewardTask(playerCode, CCReason.成就, task);

						module.getTaskCodes().remove(taskCode);
						AchievementModule.set(playerCode, module);

						response.setCode(taskCode);
						response.addAllRewards(new CurrencyHandle().packItems(rewards));

						return Result.Success;
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.DataError;
		} finally {
			Task.unlockTask(playerCode);
			AchievementModule.unlockAch(playerCode);
		}
		return Result.ParamError;
	}

}
